﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestaurantManagement.Controls;
using WindowsFormsView;
using WindowsFormsPresenter.Province;
using WindowsFormsModel.BusinessObjects;
using DevExpress.XtraEditors;

namespace RestaurantManagement.Modules.Category
{
    public partial class uProvince : BaseModule, IProvincesView, IProvinceView
    {
        #region Presenter

        private ProvincePresenter _provincePresenter;
        private ProvincesPresenter _provincesPresenter;

        #endregion

        #region Field

        public IList<ProvinceModel> Provinces
        {
            set
            {
                var Provinces = value;
                AddUserGroupToGrid(Provinces);
            }
        }

        public string ProvinceCode
        {
            get { return txtProvinceCode.Text; }
            set { txtProvinceCode.Text = value; }
        }

        public string ProvinceName
        {
            get { return txtProvinceName.Text; }
            set { txtProvinceName.Text = value; }
        }

        public DateTime CreateDate
        {
            get
            {
                if (dtCreateDate.EditValue != null) { return dtCreateDate.DateTime; }
                else { return dtCreateDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtCreateDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtCreateDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public DateTime ModifiedDate
        {
            get
            {
                if (dtModifiedDate.EditValue != null) { return dtModifiedDate.DateTime; }
                else { return dtModifiedDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtModifiedDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtModifiedDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.EditValue = value; }
        }

        #endregion

        #region Style Button

        private void btnAddNew_MouseHover(object sender, EventArgs e)
        {
            btnAddNew.BackgroundImage = null;
            btnAddNew.BackgroundImageLayout = ImageLayout.Center;
            btnAddNew.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.them_icon_2);
        }

        private void btnAddNew_MouseLeave(object sender, EventArgs e)
        {
            btnAddNew.BackgroundImage = null;
            btnAddNew.BackgroundImageLayout = ImageLayout.Center;
            btnAddNew.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.them_con);
        }

        private void btnUpdate_MouseHover(object sender, EventArgs e)
        {
            btnUpdate.BackgroundImage = null;
            btnUpdate.BackgroundImageLayout = ImageLayout.Center;
            btnUpdate.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.sua_icon_2);
        }

        private void btnUpdate_MouseLeave(object sender, EventArgs e)
        {
            btnUpdate.BackgroundImage = null;
            btnUpdate.BackgroundImageLayout = ImageLayout.Center;
            btnUpdate.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.sua_icon);
        }

        private void btnDelete_MouseHover(object sender, EventArgs e)
        {
            btnDelete.BackgroundImage = null;
            btnDelete.BackgroundImageLayout = ImageLayout.Center;
            btnDelete.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.xoa_icon_2);
        }

        private void btnDelete_MouseLeave(object sender, EventArgs e)
        {
            btnDelete.BackgroundImage = null;
            btnDelete.BackgroundImageLayout = ImageLayout.Center;
            btnDelete.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.xoa_icon);
        }

        private void btnRefresh_MouseLeave(object sender, EventArgs e)
        {
            btnRefresh.BackgroundImage = null;
            btnRefresh.BackgroundImageLayout = ImageLayout.Center;
            btnRefresh.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.lammoi_icon);
        }

        private void btnRefresh_MouseHover(object sender, EventArgs e)
        {
            btnRefresh.BackgroundImage = null;
            btnRefresh.BackgroundImageLayout = ImageLayout.Center;
            btnRefresh.BackgroundImage = new Bitmap(RestaurantManagement.Properties.Resources.lammoi_icon_2);
        }

        #endregion{

        private void AddUserGroupToGrid(IList<ProvinceModel> Provinces)
        {
            if ((Provinces != null) && (Provinces.Count > 0))
            {
                grdMain.DataSource = Provinces;
                grdMain.ForceInitialize();

                for (int i = 0; i < grdMainView.Columns.Count; i++) { grdMainView.Columns[i].Visible = false; }

                grdMainView.Columns["ProvinceCode"].Visible = true;
                grdMainView.Columns["ProvinceName"].Visible = true;
                grdMainView.Columns["Description"].Visible = true;
                grdMainView.Columns["IsActive"].Visible = true;

                grdMainView.Columns["ProvinceCode"].Caption = "Mã quận, huyện";
                grdMainView.Columns["ProvinceName"].Caption = "Tên quận, huyện";
                grdMainView.Columns["Description"].Caption = "Mô tả";
                grdMainView.Columns["IsActive"].Caption = "Được sử dụng";

                grdMainView.Columns["ProvinceCode"].VisibleIndex = 0;
                grdMainView.Columns["ProvinceName"].VisibleIndex = 1;
                grdMainView.Columns["Description"].VisibleIndex = 2;
                grdMainView.Columns["IsActive"].VisibleIndex = 3;

                grdMainView.FocusedRowHandle = 0;
            }
            else
            {
                grdMain.DataSource = null;
                InitializeTextbox();
            }
        }

        private void InitializeTextbox()
        {
            txtProvinceCode.Text = null;
            txtProvinceName.Text = null;
            txtDescription.Text = null; dtCreateDate.EditValue = null;
            dtModifiedDate.EditValue = null;
        }

        public uProvince()
        {
            InitializeComponent();

            _provincePresenter = new ProvincePresenter(this);
            _provincesPresenter = new ProvincesPresenter(this);
        }

        private void grdMainView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                if (grdMainView.CalcHitInfo(e.Location).InRow && grdMainView.FocusedRowHandle >= 0)
                {
                    var province = (ProvinceModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    if (province != null) { _provincePresenter.Display(province.ProvinceCode); }
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(ProvinceCode)) || (string.IsNullOrEmpty(ProvinceName)))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            for (int i = 0; i < grdMainView.RowCount; i++)
            {
                if (txtProvinceCode.Text == grdMainView.GetRowCellValue(i, "ProvinceCode").ToString())
                {
                    XtraMessageBox.Show("Mã quận, huyện đã tồn tại trong hệ thống !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtProvinceCode.Focus();
                    return;
                }
            }

            try
            {
                _provincePresenter.Save(true);
                _provincesPresenter.Display();
                XtraMessageBox.Show("Thêm mới dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(ProvinceCode)) || (string.IsNullOrEmpty(ProvinceName)))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (txtProvinceCode.Text != grdMainView.GetFocusedRowCellValue("ProvinceCode").ToString())
            {
                XtraMessageBox.Show("Bạn không được phép sửa mã số quận, huyện !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProvinceCode.Text = grdMainView.GetFocusedRowCellValue("ProvinceCode").ToString();
                return;
            }
            try
            {
                _provincePresenter.Save(false);
                _provincesPresenter.Display();
                XtraMessageBox.Show("Sửa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            {
                try
                {
                    if (grdMainView.FocusedRowHandle >= 0)
                    {
                        var province = (ProvinceModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                        new ProvincePresenter(null).Delete(province.ProvinceCode);
                        _provincesPresenter.Display();
                        XtraMessageBox.Show("Xóa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        XtraMessageBox.Show("Bạn chưa chọn dòng để xóa !", "Nhà hàng Hương Việt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                _provincesPresenter.Display();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void grdMainView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (grdMainView.FocusedRowHandle >= 0)
                {
                    var province = (ProvinceModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    if (province != null) { _provincePresenter.Display(province.ProvinceCode); }
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void uProvince_Load(object sender, EventArgs e)
        {
            try
            {
                _provincesPresenter.Display();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}
